package edu.iscas.appcheckclient.model.actions;

import android.location.Location;

import com.google.gson.JsonObject;

import org.json.JSONObject;

import edu.iscas.appcheckclient.model.Selector;

/**
 * Created by dell on 2016/1/7.
 */
public class MockedLocation extends ParamAction{

    public MockedLocation(Location location) {
        this.name = "mockedlocation";
        this.buildJSON(location);
    }

    private void buildJSON(Location location){
        this.addParam("Provider", location.getProvider());
        this.addParam("Time", location.getTime());
        this.addParam("ElapsedRealtimeNanos", location.getElapsedRealtimeNanos());
        this.addParam("Latitude", location.getLatitude());
        this.addParam("Longitude", location.getLongitude());
        this.addParam("Altitude", location.hasAltitude());

        this.addParam("hasSpeed", location.hasSpeed());
        this.addParam("Speed", location.getSpeed());
        this.addParam("hasBearing", location.hasBearing());
        this.addParam("Bearing", location.getBearing());
        this.addParam("hasAccuracy", location.getAccuracy());

        // skip extras
        // TODO: ADD EXTRAS
        if(location.getExtras() != null){
            JsonObject extra = new JsonObject();
            for(String key : location.getExtras().keySet()){
                Object value = location.getExtras().get(key);
                if(value == null){
                    extra.add(key, null);
                }
                else if(value instanceof String){
                    extra.addProperty(key, (String) value);
                }
                else if(value instanceof Number){
                    extra.addProperty(key, (Number) value);
                }
                else if(value instanceof Boolean){
                    extra.addProperty(key, (Boolean) value);
                }
                else if(value instanceof Character){
                    extra.addProperty(key, (Character) value);
                }
                else{
                    throw new RuntimeException("unsupported type :" + value.getClass().getSimpleName() + " in location.extra");
                }
            }
            this.addParam("Extra", extra);
        }


        this.addParam("IsFromMockProvider", location.isFromMockProvider());

    }

    @Override
    public Selector getSelector() {
        return null;
    }

    @Override
    public String brewStatement(boolean paramBoolean) {
        String origin = params.toString();
        String str = "locationQueue.add(";
        //替换所有的"为\"
        str += "\"" + origin.replaceAll("\"", "\"") + "\"";
        str += ")";
        return str;
    }

    @Override
    public void setStatement(String statement) {
        this.statement = statement;
    }

    @Override
    public String getTypeString() {
        return "MockedLocation";
    }

    @Override
    public String getValue() {
        return null;
    }
}
